﻿Imports System.Data.Linq
Imports System.Linq

Public Class CContainerController
    Dim objDataContext As New TanNamChinhDataContext

    Public Function SelectAllRecord() As IQueryable(Of tblCont)
        Try
            Return objDataContext.tblConts
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function SelectContsByFile(ByVal idFile As Integer) As IQueryable
        Try
            Return From c In objDataContext.tblConts
                   Where c.ID_File = idFile
                   Select New With {c.ID_Cont, c.ContNo, c.tblHBL.HBLNo, c.tblContType.ContTypeName, c.Seal, c.Package, c.tblUnit.UnitName, c.CBM, c.tblGood.GoodName, c.NW, c.GW}
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function SelectContsByHBL(ByVal _idHBL As Integer) As IQueryable(Of tblCont)
        Try
            Return (From cont In objDataContext.tblConts
                                              Where cont.ID_HBL = _idHBL
                                              Select cont)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecordByID(ByVal _id As Integer) As tblCont
        Try
            Return (From r In objDataContext.tblConts
                    Where r.ID_Cont = _id
                    Select r).Single
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function SelectContTypeInHBL(ByVal _idHBL As Integer) As IQueryable(Of tblContType)
        Try
            Return (From cont In objDataContext.tblConts
                    Where cont.ID_HBL = _idHBL
                    Select cont.tblContType).Distinct
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblCont) As Integer
        Try
            objDataContext.tblConts.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            Return LocalTable.ID_Cont
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub UpdateRecord(ByVal LocalTable As tblCont)
        Try
            objDataContext.tblConts.Attach(LocalTable)
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecordWithoutAttach(ByVal LocalTable As tblCont)
        Try
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub DeleteRecord(ByVal _id As Integer)
        Try
            objDataContext.tblConts.DeleteOnSubmit( _
              (From r In objDataContext.tblConts _
               Where r.ID_Cont = _id).Single)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function CheckExistsContNoInFile(ByVal sContNo As String, ByVal id As Integer, ByVal idFile As Integer) As Boolean
        'cach 1
        Return objDataContext.tblConts.Any(Function(c) c.ContNo = sContNo And c.ID_Cont <> id And c.ID_File = idFile)
    End Function
    Public Function CheckExistsSealInFile(ByVal sSeal As String, ByVal id As Integer, ByVal idFile As Integer) As Boolean
        'cach 1
        Return objDataContext.tblConts.Any(Function(c) c.Seal = sSeal And c.ID_Cont <> id And c.ID_File = idFile)
    End Function
    Public Function CheckExistsContNoInHBL(ByVal sContNo As String, ByVal id As Integer, ByVal idHBL As Integer) As Boolean
        'cach 1
        Return objDataContext.tblConts.Any(Function(c) c.ContNo = sContNo And c.ID_Cont <> id And c.ID_HBL = idHBL)
    End Function
    Public Function CheckExistsSealInHBL(ByVal sSeal As String, ByVal id As Integer, ByVal idHBL As Integer) As Boolean
        'cach 1
        Return objDataContext.tblConts.Any(Function(c) c.Seal = sSeal And c.ID_Cont <> id And c.ID_HBL = idHBL)
    End Function
    Public Sub New()
        objDataContext = New TanNamChinhDataContext
    End Sub
    Public Sub New(ByVal _obj As TanNamChinhDataContext)
        objDataContext = _obj
    End Sub

#Region "HT Code"
    ''' <summary>
    ''' Lấy danh sách cont theo Transport
    ''' Created Date: 23-12-2013
    ''' </summary>
    ''' <param name="id_file"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SelectContsByTransport(ByVal id_file As Integer) As IQueryable
        Dim query = From p In objDataContext.tblConts
                    Where p.tblHBL.tblFileMBL.ID_File = id_file
                    Select New With {p.ID_Cont, p.ContNo, p.tblHBL.HBLNo, p.tblContType.ContTypeName, p.Seal, p.GetEmptyContDate, p.DeliveryDate, p.tblGood.GoodName, p.tblDriver.DriverName}
        Return query
    End Function
#End Region
End Class
